﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.Persistent;
public partial class Distributor_Maintain : PageBase
{
    #region 定义变量
    private const string NEW_PAGE = "Distributors.aspx";
    private const string SUCCESS = "操作成功！";
    private const string ERROR = "操作失败！";
    private ChainManager _ChainManager = null;  //经销商操作类
    private CHAINS _CHAINS = null;              //一个经销商对象
    private int _ChainID = -1;                  //经销商ID号
   
    #endregion

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        

        
        if (!IsPostBack)
        {
            _ChainManager = new ChainManager();

        }

        //获取经销商ID号
        int.TryParse(Request.QueryString["Did"], out _ChainID);
        if (_ChainID > 0)
        {
            _ChainManager = new ChainManager();
            _CHAINS = _ChainManager.Load(_ChainID);
        }
        else
            Status.Value = "isNew";

        if (!Page.IsPostBack)
        {
           

            if (_CHAINS != null)
            {
                Status.Value = "isEdit";//标识编辑

                Fill(_CHAINS);
            }
        }
        
    }

    /// <summary>
    /// 提交数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        string LogContent = "";
        try
        {

            if ("isNew".Equals(Status.Value))
            {
                _CHAINS = new CHAINS();
                _CHAINS.CREATETIME = long.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
            }
            
            _CHAINS.CODE = txtCode.Text.Trim();
            _CHAINS.DESCRIPTION = txtName.Text.Trim();
            _CHAINS.Address = txtAddress.Text.Trim();
            _CHAINS.Linkman = txtContactMan.Text.Trim();
            _CHAINS.Telephone = txtPhone.Text.Trim();
            _CHAINS.DOMAINID = MyStateManager.DomainID;
            _CHAINS.ACTIVE = chxValid.Checked;
            _CHAINS.LAST_UPDATE_TIME = long.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
            _CHAINS.LAST_UPDATER = MyStateManager.Name;
            _CHAINS.OrgID = int.Parse(Hidden1.Value.Replace("'", ""));

            LogContent += "经销商代码：" + _CHAINS.CODE;
            LogContent += " 公司名称：" + _CHAINS.DESCRIPTION;
            LogContent += " 部门：" + _CHAINS.OrgID;
            LogContent += " 联系人：" + _CHAINS.Linkman;
            LogContent += " 联系人电话：" + _CHAINS.Telephone;
            LogContent += " 公司地址：" + _CHAINS.Address;
            LogContent += " 有效：" + _CHAINS.ACTIVE;
            LogContent += " 最后更新人：" + _CHAINS.LAST_UPDATER;
            LogContent += " 最后更新时间：" + _CHAINS.LAST_UPDATE_TIME;

            //保存到数据库
            _ChainManager = new ChainManager();
            _ChainManager.Save(_CHAINS);
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
              Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
              this.MyStateManager.UserID, LogContent);
            ShowMessage(SUCCESS,NEW_PAGE);
           
            return;
        }
        catch (Exception ex)
        {
            ShowMessage(ERROR+ex.Message);
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
              Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
              this.MyStateManager.UserID, "经销商维护 - 异常:" + ex.Message);
            
            return;
        }
    }

    void Fill(CHAINS obj)
    {

        //初始编辑页面                    
        txtCode.Text = obj.CODE;
        txtName.Text = obj.DESCRIPTION;
        txtContactMan.Text = obj.Linkman;
        txtPhone.Text = obj.Telephone;
        txtAddress.Text = obj.Address;
        txtOrg_Edit.Text = new OrgainzationManager().Load(obj.OrgID).NAME;
        //txtOrg_Edit.Text = txtOrg.Text;
        Hidden1.Value = obj.OrgID.ToString();
        chxValid.Checked = obj.ACTIVE;

    }
}
